import json
from travel_game.models import *
from django.template.loader import Template, Context
from django.contrib.auth import authenticate
from django.contrib.auth import login as djangoLogin
from django.shortcuts import render_to_response
from django.contrib.auth.decorators import login_required
from django.core.context_processors import csrf
from django.http import HttpResponse, HttpResponseRedirect
from django.core.urlresolvers import reverse
from django.template import RequestContext
from travel_game.forms import *

#CONSTANTS
DEFAULT_PACE = 1
DEFAULT_RATIONS = 1

@login_required
def startUp(request):
    context = {}
    context.update(csrf(request))
    context['teamInfoForm'] = teamInfoForm()
    return render_to_response('html/startUpMenu.html',context,context_instance=RequestContext(request))

@login_required
def acceptTeamInfo(request):
    context = {}
    context.update(csrf(request))
    if request.method == 'POST':
        infoForm = teamInfoForm(request.POST)
        if infoForm.is_valid():
            newGame = OTG(player=request.user)
            newGame.save()
            newParty = Party(
                 distanceFromStart=0,
                 profession=infoForm.cleaned_data['profession'],
                 game=newGame,
                 rations=infoForm.cleaned_data['rations'],
                 pace=infoForm.cleaned_data['pace']
            )
            newParty.save()
            newMember = Member(
                name = infoForm.cleaned_data['mainCharacter'],
                health = 10,
                party=newParty,
                isMain = True,
                kind = CHARACTER,
                carryWeight = CHARACTER                    
            )
            newMember.save()
            for i in range(1,5):
                currName = infoForm.cleaned_data['teamMember' + str(i)]
                newMember = Member(
                    name = currName,
                    health = 10,
                    party=newParty,
                    isMain = False,
                    kind = CHARACTER,
                    carryWeight = CHARACTER                    
                )
                newMember.save()
        else:
            context['error']=True
        context['teamInfoForm']=infoForm
        t = Template('{{ teamInfoForm.as_p }}{% if error %}<div id="error"></div>{% endif %}{% csrf_token %}<button id="continue" type="button" url="{% url travel_game.views.acceptTeamInfo %}" onclick="submitTeamInfo(this);">Go!</button>')
        return HttpResponse(t.render(Context(context)))

def login(request):
    context = {}
    context.update(csrf(request))
    if request.method=='POST':
        user = authenticate(username=request.POST['username'],password=request.POST['pwd'])
        if user is not None:
            djangoLogin(request,user)
            return HttpResponseRedirect(request.POST['next'])
    context['next'] = request.GET['next']
    return render_to_response('html/login.html',context,context_instance=RequestContext(request))

def createCharacter(request):
    pass

def initProfession(request):
    pass

@login_required
def renderStore(request):
    context={}
    return render_to_response('html/independenceStore.html',context,context_instance=RequestContext(request))
        
